package com.starryx.driverschool.controller;

import com.starryx.driverschool.po.Train;
import com.starryx.driverschool.service.StudentService;
import com.starryx.driverschool.service.TrainService;
import com.starryx.driverschool.vo.TrainQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class TrainController {

    @Autowired
    private TrainService trainService;

    @Autowired
    private StudentService studentService;

    @GetMapping("/train")
    public String train(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC)
                                Pageable pageable,
                        Model model){
        model.addAttribute("page", trainService.listTrain(pageable));
        return "train";
    }

    @GetMapping("/train/input")
    public String input(Model model){
        model.addAttribute("train", new Train());
        model.addAttribute("students", studentService.listStudent());
        return "train-input";
    }

    @PostMapping("/train/save")
    public String save(Train train,
                       RedirectAttributes attributes){
        Train train1 = trainService.saveTrain(train);
        if(train1 == null){
            attributes.addFlashAttribute("message", "新增失败");
        }else{
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/train";
    }

    // 跳转到博客列表页面
    @PostMapping("/train/search")
    public String search(@PageableDefault(size = 5, sort = {"id"}, direction = Sort.Direction.DESC) Pageable pageable,
                         TrainQuery trainQuery,
                         Model model){
        model.addAttribute("page", trainService.listTrain(pageable, trainQuery));
        return "train :: trainList";       // 返回 blogList 片段，利用 thymeleaf 实现类似 ajax 那样的局部刷新
    }

    @GetMapping("/train/delete/{id}")
    public String delete(@PathVariable Long id, RedirectAttributes attributes){
        trainService.deleteTrain(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/train";
    }

    @PostMapping("/train/batchdelete")
    public String batchDelete(@RequestParam List<Long> ids, RedirectAttributes attributes){
        // 带有外键约束的表删到最后一项时会删不掉，所以要使用逻辑删除
        for(Long id : ids){
            trainService.deleteTrain(id);
        }
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/train";
    }
}
